<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<!--<el-input v-model="filters.planDetails" placeholder="计划内容"></el-input>-->
					<el-date-picker
							v-model="filters.planTime"
							type="datetime"
							placeholder="选择日期时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getCustomerDevPlans">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="customerDevPlans" border highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="50">
			</el-table-column>
			<el-table-column type="index" width="50">
			</el-table-column>
			<el-table-column prop="planTime" label="计划时间" width="100">
				<!--使用过滤器的写法 scope.row.字段名-->
				<template slot-scope="scope">
					<span>{{ scope.row.inputTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="planSubject" label="计划主题" width="100">
			</el-table-column>
			<el-table-column prop="planDetails" label="计划内容" width="100" >
			</el-table-column>
			<el-table-column prop="systemDictionaryItem.name" label="计划实施方式" width="120">
			</el-table-column>
			<el-table-column prop="potentialCustomer.name" label="潜在客户" min-width="100">
			</el-table-column>
			<el-table-column prop="inputUser" label="创建人" min-width="100">
			</el-table-column>
			<el-table-column prop="inputTime" label="创建时间" min-width="80">
				<!--使用过滤器的写法 scope.row.字段名-->
				<template slot-scope="scope">
					<span>{{ scope.row.inputTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}</span>
				</template>
			</el-table-column>
			<!--<el-table-column prop="inputTime" label="所属租户" min-width="120" sortable>-->
			<el-table-column label="操作" width="190">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
				<el-form-item label="计划时间" prop="planTime">
					<el-date-picker
							v-model="editForm.planTime"
							type="datetime"
							placeholder="选择日期时间">
						</el-date-picker>
				</el-form-item>
				<el-form-item label="计划主题" prop="planSubject">
					<el-input v-model="editForm.planSubject" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="计划内容" prop="planDetails">
					<el-input v-model="editForm.planDetails" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="计划实施方式" prop="planType">
					<el-select v-model="editForm.systemDictionaryItemId" placeholder="请选择计划实施方式" @change="selectChange">
						<el-option
								v-for="item in systemDictionaryItems"
								:key="item.id"
								:label="item.name"
								:value="item.id"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="潜在客户" prop="potentialCustomer">
					<el-select v-model="editForm.potentialCustomerId" placeholder="请选择潜在客户" @change="selectChange">
						<el-option
								v-for="item in potentialCustomers"
								:key="item.id"
								:label="item.name"
								:value="item.id"
						></el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
				<el-form-item label="计划时间" prop="planTime">
					<el-date-picker
							v-model="addForm.planTime"
							type="datetime"
							placeholder="选择日期时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="计划主题" prop="planSubject">
					<el-input v-model="addForm.planSubject" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="计划内容" prop="planDetails">
					<el-input v-model="addForm.planDetails" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="计划实施方式" prop="systemDictionaryItem">
					<el-select v-model="addForm.systemDictionaryItemId" placeholder="请选择计划实施方式">
						<el-option
								v-for="item in systemDictionaryItems"
								:key="item.id"
								:label="item.name"
								:value="item.id"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="潜在客户" prop="potentialCustomer">
					<el-select v-model="addForm.potentialCustomerId" placeholder="请选择潜在客户">
						<el-option
								v-for="item in potentialCustomers"
								:key="item.id"
								:label="item.name"
								:value="item.id"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="创建人" prop="inputUser">
					<el-input v-model="addForm.inputUser" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="创建时间" prop="inputTime">
					<el-date-picker
							v-model="addForm.dataPicker"
							format="yyyy-MM-dd HH:mm:ss"
					/>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

	export default {
		data() {
			return {
				filters: {
					planTime: ''
				},
				currentPage:1,
				total:0,
				sysUserName:'',
				customerDevPlans: [],
				listLoading: false,
				sels: [],//列表选中列
				potentialCustomers:[],//潜在客户对象信息
				systemDictionaryItems:[],//计划实施方式
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					planTime: [{ required: true, message: '请输入计划时间', trigger: 'blur' }],
					planSubject: [{ required: true, message: '请输入计划主题', trigger: 'blur' }],
					planDetails: [{ required: true,message: '请输入计划内容', trigger: 'blur' }],
					systemDictionaryItemId: [{ required: true, message: '请输入计划实施方式', trigger: 'blur' }],
					potentialCustomerId: [{ required: true, message: '请输入潜在客户对象', trigger: 'blur' }],
					inputUser: [{ required: true, message: '请输入创建人', trigger: 'blur' }]
				},
				//编辑界面数据
				editForm: {
					id:0,//需要id传到后台
					planTime:'',
					planSubject:'',
					planDetails:'',
					systemDictionaryItemId:'',
					potentialCustomerId:'',
					inputUser:this.sysUserName
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,//关闭加载特效
				addFormRules: {
					planTime: [{ required: true, message: '请输入计划时间', trigger: 'blur' }],
					planSubject: [{ required: true, message: '请输入计划主题', trigger: 'blur' }],
					planDetails: [{ required: true,message: '请输入计划内容', trigger: 'blur' }],
					systemDictionaryItemId: [{ required: true, message: '请输入计划实施方式', trigger: 'blur' }],
					potentialCustomerId: [{ required: true, message: '请输入潜在客户对象', trigger: 'blur' }],
					inputUser: [{ required: true, message: '请输入创建人', trigger: 'blur' }]
				},
				//新增界面数据
				addForm: {
					planTime:'',
					planSubject:'',
					planDetails:'',
					systemDictionaryItemId:'',
					potentialCustomerId:'',
					inputUser:this.sysUserName,
					dataPicker:new Date//添加框里的日期
				}

			}
		},
		methods: {
			selectChange(val){
				this.$forceUpdate();
			},
			getsystemDictionaryItems(){//计划实施方式下拉框
				this.$http.patch("/systemDictionaryItem/planType").then(res =>{
					this.systemDictionaryItems = res.data;
				})
			},
			getpotentialCustomers(){//潜在客户下拉框
				this.$http.patch("/potentialCustomer/list").then(res =>{
					this.potentialCustomers = res.data;
				})
			},
			handleCurrentChange(val) {
				//当前页
				this.currentPage = val;
				this.getCustomerDevPlans();
			},
			//获取用户列表
			getCustomerDevPlans() {
				this.listLoading = true;
				let param = {
					planTime:this.filters.planTime,
					currentPage:this.currentPage
				};
				console.log(param)
				this.$http.patch("/customerDevPlan/queryPage",param).then((res) => {
					this.total = res.data.total;
					this.customerDevPlans = res.data.rows;
					 console.log(this.customerDevPlans);
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let id = row.id;//传到后台的id
					this.$http.delete("/customerDevPlan/delete/"+id).then((res) => {
						this.listLoading = false;
						let {success,msg} = res.data;
						if(success){
							this.$message({
								message: msg,
								type: 'success'
							});
							this.getCustomerDevPlans();
						}else {
							this.$message({
								message: msg,
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				// console.log(row)
				this.editForm = Object.assign({}, row);//回显
				this.editForm.systemDictionaryItemId = row.systemDictionaryItem.id;
				this.editForm.potentialCustomerId = row.potentialCustomer.id;
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm.systemDictionaryItemId = null;
				this.addForm.potentialCustomerId = null;
				this.addForm.inputUser = this.sysUserName;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							this.$http.post("/customerDevPlan/update",para).then((res) => {
								this.editLoading = false;
								let{success,msg}=res.data;
								if(success){
									this.$message({
										message: msg,
										type: 'success'
									});
									//清空form表单
									this.$refs['editForm'].resetFields();
									//关闭修改页面
									this.editFormVisible = false;
									//刷新界面数据
									this.getCustomerDevPlans();
								}else {
									this.$message({
										message: msg,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							// console.log(para)
							para.systemDictionaryItem={
								id:para.systemDictionaryItemId
							};
							para.potentialCustomer={
								id:para.potentialCustomerId
							};
							this.$http.put("/customerDevPlan/save",para).then((res) => {
								this.addLoading = false;
								let{success,msg} = res.data;
								//判断是否成功
								if(success){
									this.$message({
										message: msg,
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getCustomerDevPlans();
								}else {
									this.$message({
										message: msg,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					this.$http.patch("customerDevPlan/batchDelete",para).then((res) => {
						this.listLoading = false;
						let{success,msg} = res.data;
						if(success){
							this.$message({
								message: msg,
								type: 'success'
							});
							this.getPotentialCustomers();
						}else {
							this.$message({
								message: msg,
								type: 'success'
							});
						}
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getCustomerDevPlans();
			this.getsystemDictionaryItems();
			this.getpotentialCustomers();
			var user = sessionStorage.getItem('user');
			if (user) {
				user = JSON.parse(user);
				this.sysUserName = user.username || '';
			}
		}
	}

</script>

<style scoped>

</style>